package com.jason.java.lib;

/**
 * @description: TestQueue
 * @author: ChenBaoYang
 * @createDate: 2022/2/14 12:26 下午
 */
public class TestQueue {

    static class MyCircularQueue {

        private int[] datas;
        private int head = -1;
        private int tail = -1;
        private int length;

        public MyCircularQueue(int k) {
            datas = new int[k];
            length = k;
        }

        public boolean enQueue(int value) {
            if(isFull()) {
                return false;
            }

            if(isEmpty()) {
                head = 0;
            }
            tail = (tail + 1) % length;
            datas[tail] = value;
            return true;
        }

        public boolean deQueue() {
            if(isEmpty()) {
                return false;
            }
            if(head == tail) {
                head = -1;
                tail = -1;
                return true;
            }
            head = (head+1)%length;
            return true;
        }

        public int Front() {
            if(isEmpty()) {
                return -1;
            }
            return datas[head];
        }

        public int Rear() {
            if(isEmpty()) {
                return -1;
            }
            return datas[tail];
        }

        public boolean isEmpty() {
            return head == -1 ;
        }

        public boolean isFull() {
            return  ((tail + 1) % length) == head;
        }
    }

    public static void main(String[] args) {
        MyCircularQueue obj = new MyCircularQueue(5);
        boolean param_1 = obj.enQueue(6);
        int param1 = obj.Rear();
        int param2 = obj.Rear();
        boolean d1 = obj.deQueue();
        int param_3 = obj.Front();
        int param_4 = obj.Rear();
        boolean param_5 = obj.isEmpty();
        boolean param_6 = obj.isFull();
    }
}
